package com.ruoyi.mongo.config;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.connection.SocketSettings;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

import java.util.concurrent.TimeUnit;

@Configuration(enforceUniqueMethods = false)
public class MongoConfig {

    private static final String mongo_uri = "mongodb://1.92.74.42:7210/word";

    private final MongoCustomProperties mongoCustomProperties;

    // 注入自定义配置属性
    public MongoConfig(MongoCustomProperties mongoCustomProperties) {
        this.mongoCustomProperties = mongoCustomProperties;
    }

    // 主数据库连接工厂
    @Primary
    @Bean(name = "primaryMongoDbFactory")
    public MongoDatabaseFactory primaryMongoDbFactory() {
        // 使用自定义配置中的primary属性
        String uri = mongoCustomProperties.getPrimary().getUri();
        return new SimpleMongoClientDatabaseFactory(uri);
    }

    // 主数据库连接工厂
    @Primary
    @Bean(name = "primaryMongoDbFactory")
    public MongoDatabaseFactory primaryMongoDbFactory(
            @Qualifier("primaryMongoProperties") MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

    // 主数据库 MongoTemplate
    @Primary
    @Bean(name = "primaryMongoTemplate")
    public MongoTemplate primaryMongoTemplate(
            @Qualifier("primaryMongoDbFactory") MongoDatabaseFactory databaseFactory) {
        return new MongoTemplate(databaseFactory);
    }

    // 第二个数据库连接工厂
    @Bean(name = "secondaryMongoDbFactory")
    public MongoDatabaseFactory secondaryMongoDbFactory() {
        String uri = mongoCustomProperties.getSecondary().getUri();
        return new SimpleMongoClientDatabaseFactory(uri);
    }

    // 第二个数据库连接工厂
    @Bean(name = "secondaryMongoDbFactory")
    public MongoDatabaseFactory secondaryMongoDbFactory(
            @Qualifier("secondaryMongoProperties") MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

    // 第二个数据库 MongoTemplate
    @Bean(name = "secondaryMongoTemplate")
    public MongoTemplate secondaryMongoTemplate(
            @Qualifier("secondaryMongoDbFactory") MongoDatabaseFactory databaseFactory) {
        return new MongoTemplate(databaseFactory);
    }

    @Bean(name = "comprehensiveFactory")
    public MongoDatabaseFactory comprehensiveFactory() {
        String uri = mongoCustomProperties.getComprehensive().getUri();
        return new SimpleMongoClientDatabaseFactory(uri);
    }

    @Bean(name = "comprehensiveFactory")
    public MongoDatabaseFactory comprehensiveFactory(
            @Qualifier("comprehensiveFactory") MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

    @Bean(name = "comprehensiveTemplate")
    public MongoTemplate comprehensiveMongoTemplate(
            @Qualifier("comprehensiveFactory") MongoDatabaseFactory databaseFactory) {
        return new MongoTemplate(databaseFactory);
    }


    @Bean
    public MongoClientSettings mongoClientSettings() {
        ConnectionString connectionString = new ConnectionString(mongo_uri);

        return MongoClientSettings.builder()
                .applyConnectionString(connectionString)
                .applyToSocketSettings(builder -> {
                    builder.applySettings(SocketSettings.builder()
                            .readTimeout(90, TimeUnit.SECONDS) // 设置读取超时
                            .connectTimeout(60, TimeUnit.SECONDS) // 连接超时
                            .build());
                })
                .applyToConnectionPoolSettings(builder -> {
                    builder.maxSize(100) // 连接池最大连接数
                            .minSize(10)
                            .maxWaitTime(120, TimeUnit.SECONDS); // 获取连接的最大等待时间
                })
                .build();
    }
}
